package com.wuye.basic.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.basic.domain.WyUnit;
import com.wuye.basic.service.IWyUnitService;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 单元Controller
 * 
 * @author avalon
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/basic/unit")
public class WyUnitController extends BaseController
{
    @Autowired
    private IWyUnitService wyUnitService;

    /**
     * 查询单元列表
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyUnit wyUnit)
    {
        startPage();
        List<WyUnit> list = wyUnitService.query().list();
        return getDataTable(list);
    }

    /**
     * 导出单元列表
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:export')")
    @Log(title = "单元", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyUnit wyUnit)
    {
        List<WyUnit> list = wyUnitService.selectWyUnitList(wyUnit);
        ExcelUtil<WyUnit> util = new ExcelUtil<WyUnit>(WyUnit.class);
        util.exportExcel(response, list, "单元数据");
    }

    /**
     * 获取单元详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId)
    {
        return success(wyUnitService.selectWyUnitByUnitId(unitId));
    }

    /**
     * 新增单元
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:add')")
    @Log(title = "单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyUnit wyUnit)
    {
        return toAjax(wyUnitService.insertWyUnit(wyUnit));
    }

    /**
     * 修改单元
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:edit')")
    @Log(title = "单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyUnit wyUnit)
    {
        return toAjax(wyUnitService.updateWyUnit(wyUnit));
    }

    /**
     * 删除单元
     */
    @PreAuthorize("@ss.hasPermi('basic:unit:remove')")
    @Log(title = "单元", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        return toAjax(wyUnitService.removeBatchByIds(Arrays.asList(unitIds)));
    }
}
